﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Wormhole.UI.Application.Controls.Generic
{
	/// <summary>
	/// Interaction logic for UserInput.xaml
	/// </summary>
	public partial class UserInput : UserControl
	{
		#region Properties

		#region Player
		private Engine.Player.Player _player;
		/// <summary>
		/// The game engine object holding the player score.
		/// </summary>
		public Engine.Player.Player Player
		{
			get
			{
				return this._player;
			}

			set
			{
				if (this._player != value)
				{
					this._player = value;
					this.SetEngineEvents();
				}
			}
		}
		#endregion

		#region Request
		/// <summary>
		/// The object containing details for the input request.
		/// </summary>
		public Wormhole.Engine.Input.Request Request { get; set; }
		#endregion

		#endregion

		#region Constructor
		/// <summary>
		/// Initialize a default instance of the class.
		/// </summary>
		public UserInput()
		{
			InitializeComponent();
		}
		#endregion

		#region Set Engine Events
		/// <summary>
		/// Wire up the events for the engine objects.
		/// </summary>
		protected void SetEngineEvents()
		{
			this.Player.UserInput.GetInput += new Wormhole.Engine.Input.UserInput.GetInputDelegate(UserInput_GetInput);
			this.Player.AllCards.CardSelected += new Wormhole.Engine.CardLibrary.BaseCardTypes.Card.CardSelectedDelegate(AllCards_CardSelected);
		}
		#endregion

		#region Event Handlers

		#region Get Input
		/// <summary>
		/// The event handler for when user input is requested.
		/// </summary>
		/// <param name="request">Details for the request</param>
		private void UserInput_GetInput(Wormhole.Engine.Input.Request request)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate()
			{
				this.Request = request;
				this.SetupInput();
			});
		}
		#endregion

		#region Button Click
		/// <summary>
		/// This is the event handler for the single button (normally the pass button).
		/// </summary>
		private void Button_Click(object sender, RoutedEventArgs e)
		{
			this.OnButton();
		}
		#endregion

		#region  Yes Click
		/// <summary>
		/// The event handler for the yes button.
		/// </summary>
		private void btnYes_Click(object sender, RoutedEventArgs e)
		{
			this.OnYes();
		}
		#endregion

		#region No Click
		/// <summary>
		/// The event handler for the no button.
		/// </summary>
		private void btnNo_Click(object sender, RoutedEventArgs e)
		{
			this.OnNo();
		}
		#endregion

		#region Card Selected
		/// <summary>
		/// Handle the card selected event.
		/// </summary>
		/// <param name="card"></param>
		private void AllCards_CardSelected(Wormhole.Engine.CardLibrary.BaseCardTypes.Card card)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate()
			{
				if (this.Request.ResponseType == Wormhole.Engine.Input.ResponseType.SingleCardSelect)
					this.OnSingleCardSelected(card);
			});
		}
		#endregion

		#endregion

		#region Setup Input
		/// <summary>
		/// Set up the control for user input.
		/// </summary>
		protected void SetupInput()
		{
			this.gridPass.Visibility = Visibility.Hidden;
			this.gridYesNo.Visibility = Visibility.Hidden;

			this.lblMessage.Text = this.Request.Message;

			if (this.Request.ResponseType == Wormhole.Engine.Input.ResponseType.None)
			{
			}
			else if (this.Request.ResponseType == Wormhole.Engine.Input.ResponseType.YesNo)
			{
				// show the appropriate grid
				this.gridPass.Visibility = Visibility.Hidden;
				this.gridYesNo.Visibility = Visibility.Visible;

				// set the button text
				this.btnYes.Content = "Yes";
				this.btnNo.Content = "No";
			}
			else if (this.Request.ResponseType == Wormhole.Engine.Input.ResponseType.Pass)
			{
				// show the appropriate grid
				this.gridPass.Visibility = Visibility.Visible;
				this.gridYesNo.Visibility = Visibility.Hidden;

				// set the button text
				this.btnPass.Content = "Pass";
			}
		}
		#endregion

		#region On Yes
		/// <summary>
		/// Handle the yes button click.
		/// </summary>
		protected void OnYes()
		{
			Wormhole.Engine.Input.Response response = new Wormhole.Engine.Input.Response();

			if (this.Request.ResponseType == Wormhole.Engine.Input.ResponseType.YesNo)
			{
				response.ResponseType = Wormhole.Engine.Input.ResponseType.Yes;
			}

			// raise the callback
			if (this.Request.Callback != null)
				this.Request.Callback(response);
		}
		#endregion

		#region On No
		/// <summary>
		/// Handle the no button click.
		/// </summary>
		protected void OnNo()
		{
			Wormhole.Engine.Input.Response response = new Wormhole.Engine.Input.Response();

			if (this.Request.ResponseType == Wormhole.Engine.Input.ResponseType.YesNo)
			{
				response.ResponseType = Wormhole.Engine.Input.ResponseType.No;
			}

			// raise the callback
			if (this.Request.Callback != null)
				this.Request.Callback(response);
		}
		#endregion

		#region On Button
		/// <summary>
		/// Handle the singe button (pass button) click.
		/// </summary>
		protected void OnButton()
		{
			Wormhole.Engine.Input.Response response = new Wormhole.Engine.Input.Response();
			
			// raise the callback
			if (this.Request.Callback != null)
				this.Request.Callback(response);
		}
		#endregion

		#region On Single Card Selected
		/// <summary>
		/// Handle a single card selection.
		/// </summary>
		/// <param name="card"></param>
		protected void OnSingleCardSelected(Wormhole.Engine.CardLibrary.BaseCardTypes.Card card)
		{
			Wormhole.Engine.Input.Response response = new Wormhole.Engine.Input.Response();
			response.ResponseType = Wormhole.Engine.Input.ResponseType.SingleCardSelect;
			
			response.SelectedCards = new Wormhole.Engine.Decks.Deck();
			response.SelectedCards.Add(card);

			// raise the callback
			if (this.Request.Callback != null)
				this.Request.Callback(response);
		}
		#endregion
	}
}
